import { _decorator, Button, Component, EditBox, Label, Node } from 'cc';
import { NodeName } from '../../utils/common';
import { camelToSnakeCase } from '../../utils/func';
const { ccclass, property } = _decorator;

@ccclass('StreamerQueryFormControl')
export class StreamerQueryFormControl extends Component {
    private submit_button: Node = null;

    private submit_form: object = {};

    protected onLoad(): void {
        this.submit_button = this.node.getChildByName(NodeName.StreamerQueryScene.Widget.SubmitWidget).getChildByName(NodeName.StreamerQueryScene.Button.SubmitButton);

        this.submit_button.on(Button.EventType.CLICK, this.onSubmitButtonClick, this);
    }

    protected onDestroy(): void {
        if (this.submit_button !== null && this.submit_button !== undefined && this.submit_button.name.length) {
            this.submit_button.off(Button.EventType.CLICK, this.onSubmitButtonClick, this);
        }
    }

    onSubmitButtonClick() {
        this.node.getComponentsInChildren(Button).forEach((button) => {
            if (button.node.name === NodeName.StreamerQueryScene.Button.LivePlatformButton) {
                if (button.node.getChildByName('Label').getComponent(Label).string === '请选择直播平台信息') {
                    this.submit_form['live_platform'] = '';
                } else {
                    this.submit_form['live_platform'] = parseInt(button.node.getChildByName('Label').getComponent(Label).string.split('-')[0], 10);
                }
            }
        })
        this.node.getComponentsInChildren(EditBox).forEach((editbox) => {
            this.submit_form[camelToSnakeCase(editbox.node.name.replace('EditBox', ''))] = editbox.getComponent(EditBox).string;
        })
        console.log('提交信息：', this.submit_form);
    }
}


